
package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.enums.MaterialBidStatus;
import com.scs.application.enums.MaterialInquiryStatus;
import com.scs.application.modules.base.entity.MaterialBid;
import com.scs.application.modules.base.entity.MaterialBidItem;
import com.scs.application.modules.base.entity.MaterialInquiry;
import com.scs.application.modules.base.mapper.MaterialBidItemMapper;
import com.scs.application.modules.base.mapper.MaterialBidMapper;
import com.scs.application.modules.base.mapper.MaterialInquiryMapper;
import com.scs.application.modules.base.request.MaterialBidQueryRequest;
import com.scs.application.modules.base.service.MaterialBidItemService;
import com.scs.application.modules.base.service.MaterialBidService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 耗材报价单 service 实现
 */
@Slf4j
@AllArgsConstructor
@Service
@Transactional(rollbackFor = Exception.class)
public class MaterialBidServiceImpl extends BaseServiceImpl<MaterialBidMapper, MaterialBid> implements MaterialBidService {

    private final MaterialBidItemService materialBidItemService;

    private final MaterialBidItemMapper materialBidItemMapper;

    private final MaterialInquiryMapper materialInquiryMapper;

    @Override
    public boolean removeCascadeById(Serializable id) {
        materialBidItemMapper.delete(Wrappers.<MaterialBidItem>query().eq("bid_id", id));
        return super.removeById(id);
    }

    @Override
    public List<MaterialBid> list(MaterialBidQueryRequest request) {
        if (UserUtils.isSupplier()) {
            request.setSupplierId(UserUtils.getSupplierId());
        } else if (UserUtils.isHospital()) {
            request.setHospitalId(UserUtils.getHospitalId());
        }
        //request.setSupplierId(UserUtils.getUser().isSupplier() ? UserUtils.getSupplierId() : null);
        List<MaterialBid> list = this.baseMapper.list(request);
        return list;
    }

    @Override
    public IPage<MaterialBid> page(MaterialBidQueryRequest request) {
        if (UserUtils.isSupplier()) {
            request.setSupplierId(UserUtils.getSupplierId());
        } else if (UserUtils.isHospital()) {
            request.setHospitalId(UserUtils.getHospitalId());
        }
        //request.setSupplierId(UserUtils.getUser().isSupplier() ? UserUtils.getSupplierId() : null);
        IPage page = this.baseMapper.page(request.getPage(), request);
        return page;
    }

    @Override
    public void submitMaterialBid(String id) {
        MaterialBid mb = getById(id);

        if (mb == null) {
            return;
        }

        //当前报价单状态是否是未提交状态
        if (!MaterialBidStatus.UNSUBMIT.getValue().equals(mb.getFlowStatus())) {
            throw new BusinessException("当前报价单状态不是未提交状态，无法提交");
        }

        //明细是否为空
        List<MaterialBidItem> list = materialBidItemMapper.selectList(Wrappers.<MaterialBidItem>query().eq("bid_id", id));
        if (list.isEmpty()) {
            throw new BusinessException("报价单明细为空，不允许提交");
        }

        //状态修改为已提交状态
        MaterialBid materialBid = new MaterialBid();
        materialBid.setFlowStatus(MaterialBidStatus.SUBMITTED.getValue())
                .setId(id);
        updateById(materialBid);
    }

    /**
     * 撤销报价单
     **/
    @Override
    public void cancelMaterialBid(String id) {
        MaterialBid materialBid = getById(id);
        if (materialBid == null) {
            return;
        }
        MaterialInquiry materialInquiry = materialInquiryMapper.selectById(materialBid.getInquiryId());
        //当前询价单状态是否是piblished状态
        if (!MaterialInquiryStatus.PUBLISHED.getValue().equals(materialInquiry.getFlowStatus())) {
            throw new BusinessException("当前询价单状态不是已发布状态，无法撤销");
        } else if (MaterialBidStatus.SUBMITTED.getValue().equals(materialBid.getFlowStatus()) && MaterialInquiryStatus.PUBLISHED.getValue().equals(materialInquiry.getFlowStatus())) {
            //撤销报价单状态为unsubmit
            MaterialBid mb = new MaterialBid();
            mb.setFlowStatus(MaterialBidStatus.UNSUBMIT.getValue())
                    .setId(id);
            updateById(mb);
            //撤销报价单明细
            /*List<MaterialBidItem> mbiList = materialBidItemMapper.selectList(Wrappers.<MaterialBidItem>query().eq("bid_id", material_bid.getId()));
            mbiList.forEach(o -> o.setSkuPriceBid((double) 0.00).setBidRemark(""));
            materialBidItemService.updateBatchById(mbiList);*/
        }
    }

    private QueryWrapper<MaterialBid> getQueryWrapper(MaterialBidQueryRequest request) {
        return Wrappers.<MaterialBid>query()
                .like(StringUtils.isNotBlank(request.getInquiryKey()), "inquiry_key", request.getInquiryKey())
                .like(StringUtils.isNotBlank(request.getSupplierName()), "supplier_name", request.getSupplierName())
                .eq(StringUtils.isNotBlank(request.getSupplierId()), "supplier_id", request.getSupplierId())
                .eq(StringUtils.isNotBlank(request.getHospitalId()), "hospital_id", request.getHospitalId())
                .eq(StringUtils.isNotBlank(request.getFlowStatus()), "flow_status", request.getFlowStatus())
                .orderByDesc("gmt_modified");
    }

}
